package com.hancher.common.rv;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.hancher.common.R;
import com.hancher.common.androidutil.DensityUtil;
import com.hancher.common.androidutil.LogUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2021/7/12 0012 22:30 <br/>
 * 描述 : 用于重复的初始化rv, 更新data<br/>
 * adapter.setOnItemClickListener 单击<br/>
 * adapter.setOnItemLongClickListener 长按<br/>
 * adapter.getLoadMoreModule().setOnLoadMoreListener 上滑<br/>
 * swipeRefresh.setOnRefreshListener 下拉
 */
@Data
@NoArgsConstructor
public class PageRecyclerViewManager<A> {
    public static final String FRESH = "FRESH";
    public static final String LOADMORE = "LOADMORE";
    String dataChangeType = FRESH;
    Context context;
    RecyclerView recyclerView;
    BaseVBAdapter adapter;
    SmartRefreshLayout refreshLayout;
    OnRefreshLoadMoreListener refreshListener;
    boolean enableRefresh = false;
    boolean enableLoadMore = false;
    int col = 1;
    boolean enableEmptyView = false;
    boolean enableAnimator = false;
    boolean enablePage = false;

    View.OnClickListener emptyOnClickListener;
    private int page = 1, size = 10, count = 0;
    private List<A> rvList = new ArrayList<>();
    private BaseQuickAdapter.OnItemClickListener onItemClickListener;
    private boolean isDivider = false;
    boolean isHorizontal = false;
    private HashMap<Integer, BaseQuickAdapter.OnItemChildClickListener> onItemChildClickListenerHashMap = new HashMap<>();
    private BaseQuickAdapter.OnItemLongClickListener onItemLongClickListener;

    public PageRecyclerViewManager<A> init() {
        if (col == 1) {
            LinearLayoutManager llm = new LinearLayoutManager(context);
            llm.setOrientation(isHorizontal ? RecyclerView.HORIZONTAL : RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(llm);
        } else {
            GridLayoutManager llm = new GridLayoutManager(context, col);
            llm.setOrientation(isHorizontal ? RecyclerView.HORIZONTAL : RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(llm);
        }
        if (refreshLayout != null) {
            if (refreshListener != null) {
                refreshLayout.setOnRefreshLoadMoreListener(refreshListener);
                refreshLayout.setEnableRefresh(enableRefresh);
                refreshLayout.setEnableLoadMore(enableLoadMore);
            } else {
                refreshLayout.setEnableRefresh(false);
                refreshLayout.setEnableLoadMore(false);
            }
        }
        if (!enableAnimator && recyclerView.getItemAnimator() != null) {
            recyclerView.getItemAnimator().setAddDuration(0);
            recyclerView.getItemAnimator().setChangeDuration(0);
            recyclerView.getItemAnimator().setMoveDuration(0);
            recyclerView.getItemAnimator().setRemoveDuration(0);
            ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        }
        adapter.setStateViewEnable(enableEmptyView);
//        adapter.setEmptyViewEnable(enableEmptyView);
        if (enableEmptyView) {
//            View notDataView = activity.getLayoutInflater().inflate(R.layout.empty,
//                    recyclerView, false);
//            if (emptyOnClickListener != null) {
//                notDataView.setOnClickListener(emptyOnClickListener);
//            }
//            adapter.setEmptyView(notDataView);
            adapter.setStateViewLayout(context, R.layout.empty);
            if (emptyOnClickListener != null) {
                adapter.getStateView().setOnClickListener(emptyOnClickListener);
            }
        }
        if (isDivider) {
            recyclerView.addItemDecoration(new RecycleViewDivider(context, DividerItemDecoration.VERTICAL,
                    DensityUtil.dp2px(context, 1f), Color.parseColor("#eeeeee")));
            recyclerView.addItemDecoration(new RecycleViewDivider(context, DividerItemDecoration.HORIZONTAL,
                    DensityUtil.dp2px(context, 1f), Color.parseColor("#eeeeee")));
        }

        adapter.setAnimationEnable(true);
        adapter.setOnItemClickListener(onItemClickListener);
        for (Map.Entry<Integer, BaseQuickAdapter.OnItemChildClickListener> item : onItemChildClickListenerHashMap.entrySet()) {
            adapter.addOnItemChildClickListener(item.getKey(), item.getValue());
        }
        adapter.setOnItemLongClickListener(onItemLongClickListener);
        recyclerView.setAdapter(adapter);
        return this;
    }

    public PageRecyclerViewManager<A> addOnItemChildClickListener(int id, BaseQuickAdapter.OnItemChildClickListener listener) {
        onItemChildClickListenerHashMap.put(id, listener);
        return this;
    }

    public void freshData(List<A> list) {
        rvList.clear();
        rvList.addAll(list);
        LogUtil.v("data size=" + rvList.size());
        adapter.submitList(rvList);
    }

    public void freshData() {
        LogUtil.v("data size=" + rvList.size());
        adapter.submitList(rvList);
    }

    public void freshFail() {
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(false);
    }

    /**
     * 更新数据
     *
     * @param list
     * @param totalCount
     */
    public void freshPageData(List<A> list, int totalCount) {
        count = totalCount;
        if (count <= 0) {
            rvList.clear();
            refreshLayout.finishRefresh(true);
            refreshLayout.setEnableLoadMore(false);
            LogUtil.i("dataChangeType=" + dataChangeType + ", data size=" + rvList.size());
            adapter.submitList(rvList);
            return;
        }
        refreshLayout.setNoMoreData(false);
        refreshLayout.setEnableLoadMore(true);
        switch (dataChangeType) {
            case FRESH:
                rvList.clear();
                rvList.addAll(list);
                refreshLayout.finishRefresh(true);
                refreshLayout.setEnableLoadMore(rvList.size() < count);
                break;
            case LOADMORE:
                rvList.addAll(list);
                if (rvList.size() < count) {
                    refreshLayout.finishLoadMore();
                } else {
                    //显示全部加载完成，并不再触发加载更事件
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
                break;
            default:
        }
        LogUtil.d("dataChangeType=" + dataChangeType + ", data size=" + rvList.size() + ", total count=" + count);
        adapter.submitList(rvList);
    }

    public String getPageStr() {
        return page + "";
    }

    public String getSizeStr() {
        return size + "";
    }

    public PageRecyclerViewManager<A> setDataChangeType(String dataChangeType) {
        this.dataChangeType = dataChangeType;
        if (FRESH.equals(dataChangeType)) {
            page = 1;
        } else if (LOADMORE.equals(dataChangeType)) {
            page++;
        } else {
            LogUtil.d("err:不支持其他刷新方式");
        }
        return this;
    }

    public PageRecyclerViewManager<A> setContext(Context context) {
        this.context = context;
        return this;
    }

    public PageRecyclerViewManager<A> setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        return this;
    }

    public PageRecyclerViewManager<A> setAdapter(BaseVBAdapter adapter) {
        this.adapter = adapter;
        return this;
    }

    public PageRecyclerViewManager<A> setRefreshLayout(SmartRefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;
        return this;
    }

    public PageRecyclerViewManager<A> setRefreshListener(OnRefreshLoadMoreListener refreshListener) {
        this.refreshListener = refreshListener;
        return this;
    }

    public PageRecyclerViewManager<A> setEnableRefresh(boolean enableRefresh) {
        this.enableRefresh = enableRefresh;
        return this;
    }

    public PageRecyclerViewManager<A> setEnableLoadMore(boolean enableLoadMore) {
        this.enableLoadMore = enableLoadMore;
        return this;
    }

    public PageRecyclerViewManager<A> setCol(int col) {
        this.col = col;
        return this;
    }

    public PageRecyclerViewManager<A> setEnableEmptyView(boolean enableEmptyView) {
        this.enableEmptyView = enableEmptyView;
        return this;
    }

    public PageRecyclerViewManager<A> setEnableAnimator(boolean enableAnimator) {
        this.enableAnimator = enableAnimator;
        return this;
    }

    public PageRecyclerViewManager<A> setEnablePage(boolean enablePage) {
        this.enablePage = enablePage;
        return this;
    }

    public PageRecyclerViewManager<A> setEmptyOnClickListener(View.OnClickListener emptyOnClickListener) {
        this.emptyOnClickListener = emptyOnClickListener;
        return this;
    }

    public PageRecyclerViewManager<A> setPage(int page) {
        this.page = page;
        return this;
    }

    public PageRecyclerViewManager<A> setSize(int size) {
        this.size = size;
        return this;
    }

    public PageRecyclerViewManager<A> setCount(int count) {
        this.count = count;
        return this;
    }

    public PageRecyclerViewManager<A> setRvList(List<A> rvList) {
        this.rvList = rvList;
        return this;
    }

    public PageRecyclerViewManager<A> setOnItemClickListener(BaseQuickAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return this;
    }

    public PageRecyclerViewManager<A> setDivider(boolean divider) {
        isDivider = divider;
        return this;
    }

    public PageRecyclerViewManager<A> setHorizontal(boolean horizontal) {
        isHorizontal = horizontal;
        return this;
    }

    public PageRecyclerViewManager<A> setOnItemChildClickListenerHashMap(HashMap<Integer, BaseQuickAdapter.OnItemChildClickListener> onItemChildClickListenerHashMap) {
        this.onItemChildClickListenerHashMap = onItemChildClickListenerHashMap;
        return this;
    }

    public PageRecyclerViewManager<A> setOnItemLongClickListener(BaseQuickAdapter.OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
        return this;
    }
}
